//
//  AddPersonHeaderView.m
//  Findif
//
//  Created by Mac on 2019/4/15.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "AddPersonHeaderView.h"

@interface AddPersonHeaderView ()

@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, strong)UIButton * ageButton;
@property (nonatomic, strong)UIButton * genderButton;

@end

@implementation AddPersonHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"附近的人";
    titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    titleLabel.textColor = ZRBlackColor;
    
    self.titleLabel = titleLabel;
    
    [self addSubview:self.titleLabel];
    
    
    self.ageButton = [self setupButton];
    [self addSubview:self.ageButton];
    self.genderButton = [self setupButton];
    [self addSubview:self.genderButton];
    
    self.ageLabel = [self setupLabelWith:@"年龄"];
    [self.ageButton addSubview:self.ageLabel];
    self.genderLabel = [self setupLabelWith:@"性别"];
    [self.genderButton addSubview:self.genderLabel];
    
    self.ageImageView = [self setupImageViewWith:@"展开"];
    [self.ageButton addSubview:self.ageImageView];
    self.genderImageView = [self setupImageViewWith:@"展开"];
    [self.genderButton addSubview:self.genderImageView];
}


- (void)layoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(70 * SCREENH6);
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
    }];

    [self.ageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.leading.mas_equalTo(self.titleLabel.mas_trailing).offset(15 * SCREENH6);
        make.width.mas_equalTo(55 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];

    [self.ageImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.ageButton).offset(-9 * SCREENH6);
        make.centerY.mas_equalTo(self.ageButton);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(4 * SCREENH6);
    }];

    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.ageButton);
        make.trailing.mas_equalTo(self.ageImageView.mas_leading).offset(-4 * SCREENH6);
    }];
    
    [self.genderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.mas_equalTo(self.ageButton);
        make.width.mas_equalTo(50 * SCREENH6);
        make.leading.mas_equalTo(self.ageButton.mas_trailing).offset(20 * SCREENH6);
    }];

    [self.genderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.genderButton).offset(-9 * SCREENH6);
        make.centerY.mas_equalTo(self.genderButton);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(4 * SCREENH6);
    }];

    [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.genderButton);
        make.trailing.mas_equalTo(self.genderImageView.mas_leading).offset(-3 * SCREENH6);
    }];
}

- (UIButton *)setupButton{
    
    UIButton * button = [UIButton new];
    
    [button setBackgroundColor:ColorWithRGB(0xECECEC)];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.cornerRadius = 10 * SCREENH6;
    button.layer.masksToBounds = YES;
    
    return button;
}


- (UILabel *)setupLabelWith:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textAlignment = NSTextAlignmentRight;
    label.text = title;
    label.textColor = ZRBlackColor;
    label.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    return label;
}


- (UIImageView *)setupImageViewWith:(NSString *)img{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:img];
    
    return imageView;
}


- (void)clickButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.ageButton) {
        
        type = @"age";
        self.ageImageView.image = [UIImage imageNamed:@"收起"];
    }else{
        
        type = @"gender";
        self.genderImageView.image = [UIImage imageNamed:@"收起"];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchTypeWithType:)])
    {
        
        [self.delegate searchTypeWithType:type];
    }
}

@end
